package com.chaodev.base.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.chaodev.base.dao.AttDao;
import com.chaodev.base.dao.LearningParkDao;
import com.chaodev.base.em.BizTypeEnum;
import com.chaodev.base.model.Att;
import com.chaodev.base.model.LearningPark;
import com.chaodev.base.model.UserInfo;
import com.chaodev.base.service.LearningParkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * vcbase
 *
 * @author 95634
 * @描述
 * @date 2023/6/9 11:26
 **/
@Service
public class LearningParkServiceImpl implements LearningParkService {
    @Autowired
    LearningParkDao dao;
    @Autowired
    private AttDao attDao;

    @Override
    public LearningPark findDataById(String id) {
        LearningPark learningPark = dao.findDataById(id);
        Att att = new Att();
        att.setDataNo(learningPark.getId());
        att.setState(1);
        att.setBizType(BizTypeEnum.Em_LearnPark.getValue());
        learningPark.setAtts(attDao.queryDataList(att));
        return learningPark;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void saveData(LearningPark learningPark, UserInfo userInfo) throws DataAccessException {
        if(ObjectUtil.isNotNull(learningPark.getId())&&learningPark.getId()>0){
            dao.updateDataById(learningPark);
        }else{
            dao.insertData(learningPark);
        }
        if(learningPark.getAtts()!=null&&learningPark.getAtts().size()>0){
            attDao.deleteDataByDataNo(String.valueOf(learningPark.getId()),BizTypeEnum.Em_LearnPark.getValue());
            for (Att att : learningPark.getAtts()) {
                att.setBizType(BizTypeEnum.Em_LearnPark.getValue());
                att.setDataNo(learningPark.getId());
                att.setcUser(userInfo.getUserId());
                attDao.insertData(att);
            }
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void insertData(LearningPark learningPark) throws DataAccessException{
        dao.insertData(learningPark);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void updateDataById(LearningPark learningPark) throws DataAccessException{
        dao.updateDataById(learningPark);
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void deleteDataById(String id) throws DataAccessException{
        dao.deleteDataById(id);
        attDao.deleteDataByDataNo(id,BizTypeEnum.Em_LearnPark.getValue());
    }

    @Override
    public List<LearningPark> queryDataList(LearningPark learningPark) {
        return dao.queryDataList(learningPark);
    }

    @Override
    public PageInfo<LearningPark> queryPageList(int start,int limit, LearningPark tbLearningPark) {
        PageHelper.startPage(start, limit);
        List<LearningPark> parkList = dao.queryDataList(tbLearningPark);
        Att att = null;
        for (int i = 0; i < parkList.size(); i++) {
            att = new Att();
            att.setState(1);
            att.setBizType(BizTypeEnum.Em_LearnPark.getValue());
            att.setDataNo(parkList.get(i).getId());
            parkList.get(i).setAtts(attDao.queryDataList(att));
        }
        PageInfo<LearningPark> pageInfo = new PageInfo<LearningPark>(parkList, limit);
        return pageInfo;
    }

    @Override
    public void updateVisitNum(LearningPark park) {
        LearningPark dataById = dao.findDataById(park.getId() + "");
        Integer visitNum = dataById.getVisitNum() == null ? 0 : dataById.getVisitNum();
        park.setVisitNum(visitNum + 1);
        dao.updateVisitNum(park);
    }
}
